//
//  XSJEmoticonToolBar.swift
//  weiBo项目准备
//
//  Created by 鲜世杰 on 16/5/20.
//  Copyright © 2016年 鲜世杰. All rights reserved.
//

import UIKit


enum XSJEmoticonToolBarButtonType: Int {
    //  默认
    case Normal = 1000
    //  emoji
    case Emoji = 1001
    //  浪小花
    case Lxh = 1002
}



class XSJEmoticonToolBar: UIStackView {
    
    var lastSelectedButton : UIButton?
    
    var didSelectedEmoticonButtonClosure : ((type : XSJEmoticonToolBarButtonType)->())?
    
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        setUpUI()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    private func setUpUI () {
    
        axis = .Horizontal
        distribution = .FillEqually
        addChildButton("默认", bgImageName: "compose_emotion_table_left", type: .Normal)
        addChildButton("Emoji", bgImageName: "compose_emotion_table_mid", type: .Emoji)
        addChildButton("浪小花", bgImageName: "compose_emotion_table_right", type: .Lxh)
        
    }
    
    
    private func addChildButton(title: String, bgImageName: String, type: XSJEmoticonToolBarButtonType) {
    
        let button = UIButton()
        button.tag = type.rawValue

        button.addTarget(self, action: "emoticonButtonClick:", forControlEvents: .TouchUpInside)

        button.setTitle(title, forState: .Normal)
        //  设置文字颜色
        button.setTitleColor(UIColor.whiteColor(), forState: .Normal)
        button.setTitleColor(UIColor.grayColor(), forState: .Selected)
        //  字体大小
        button.titleLabel?.font = UIFont.systemFontOfSize(15)
        //  设置背景图片
        button.setBackgroundImage(UIImage(named: "\(bgImageName)_normal"), forState: .Normal)
        button.setBackgroundImage(UIImage(named: "\(bgImageName)_selected"), forState: .Selected)
        
        button.adjustsImageWhenHighlighted = false

        
        //  添加按钮
        addArrangedSubview(button)

        if type == .Normal {
            lastSelectedButton?.selected = false
            button.selected = true
            lastSelectedButton = button
        }

    }
    
    @objc private func emoticonButtonClick(button: UIButton) {
        
        if button == lastSelectedButton{
            return
        }
        
        //  1.取消上一次选中的状态
        lastSelectedButton?.selected = false
        //  2.当前点击的按钮选中
        button.selected = true
        //  3.记录选中按钮
        lastSelectedButton = button

        let type = XSJEmoticonToolBarButtonType(rawValue: button.tag)
        
        didSelectedEmoticonButtonClosure?(type: type!)
        
        
    }
    
    
    func selectEmotionButtonWithSection(section : Int) {
    
    
        let view = viewWithTag(section + 1000)
        if section + 1000 == lastSelectedButton?.tag {
        
        
        return
        
        }
        let button = viewWithTag(section + 1000) as! UIButton
        lastSelectedButton?.selected = false
        button.selected = true
        lastSelectedButton = button
    
    }
    
    
    
}
